
class Viewport:

    def __init__(self, client):
        self.x = 0
        self.y = 0
        self.client = client
        self.world = self.client.world
        self.width = int(round(self.client.width / 64))
        self.height = int(round(self.client.height / 64))
        print(str(self.width) + "/" + str(self.height))

    def update(self):
        self.x = self.world.player.x - self.width / 2
        self.y = self.world.player.y - self.height / 2
        self.renderBlocks()
        self.renderEntities()

    def renderBlocks(self):
        col = int(self.x)
        row = int(self.y)
        for block in self.world.blocks:
            if block.x >= col and block.x <= col + self.width + 1 and block.y >= row and block.y <= row + self.height + 1:
                block.render(self.client, self)

    def renderEntities(self):
        col = int(self.x)
        row = int(self.y)
        for entity in self.world.entities:
            if entity.x >= col and entity.x <= col + self.width and entity.y >= row and entity.y <= row + self.height:
                entity.render(self.client, self)

    def globalToLocale(self, x, y):
        return (x - self.x, y - self.y)

    @staticmethod
    def getViewportXRelativeToPlayer(x, width):
        return x - width / 128.

    @staticmethod
    def getViewportYRelativeToPlayer(y, height):
        return y - height / 128.
